<?php
/*********************************************
  Dragonfly CMS, Copyright (c) 2004 by DragonflyCMS Dev. Team.
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/

# http://www.php.net/manual/en/filter.filters.validate.php
abstract class Filter
{
	public static function _get()
	{
		$query = false === strpos(URL::query(), '?');
		if ($query) {
			$qs = str_replace('.html', '', URL::query());
			$qs = explode('/', $qs);
		} else {
			$qs = array();
			foreach ($_GET as $key => $val) {
				$qs[] = $key .($val ? '='.$val : '');
			}
		}
		$_GET = array();
		if ($query) {
			if (false === strpos($qs[0], '-') && preg_match('#^[a-zA-Z0-9_]+$#i', $qs[0])) {
				$_GET['name'] = $qs[0];
				unset($qs[0]);
			}
			if (!empty($qs[1]) && (false === strpos($qs[1], '=') && false === strpos($qs[1], '-'))) {
				if (preg_match('#^([a-zA-Z0-9_]+)$#', $qs[1])) {
					$_GET['file'] = $qs[1];
					unset($qs[1]);
				}
			}
		}
		foreach ($qs as $key => $val) {
			if (preg_match('#^([a-zA-Z0-9_\-]+)(?:=([a-zA-Z0-9_;:\#\-\.\(\)]+))?$#', $val, $match)) {
				$_GET[$match[1]] = empty($match[2]) ? '' : $match[2];
			}
		}
	}

	public static function username($var) {}
	public static function hostname($var) {}

	/*
		@ Return bool
	*/
	public static function email($email) {
		# Although RFC 1035 doesn't allow 1 char subdomains we allow it due to bug report 641
		return 1 === preg_match('#^[\w\.\+\-]+@(([\w]{1,25}\.)?[0-9a-z\-]{2,63}\.[a-z]{2,6}(\.[a-z]{2,6})?)$#', $email);
	}

	/*
		@ Return bool
	*/
	public static function domain($www)
	{
		if (false === strpos($www, '://')) {
			$www = (preg_match('#^([a-z0-9\-\.]+)?[a-z0-9\-]+\.[a-z]{2,4}\:443$#i', $www) ? 'https://' : 'http://') .$www;
		}
		return 1 === preg_match('#^http[s]?\:\/\/([a-z0-9\-\.]+)?[a-z0-9\-]+\.[a-z]{2,4}$#i', $www);
	}
}
